<?php

//retreive image ends

/*
Plugin Name: Excerpt Link Modifier
Plugin URI: http://aweconsulting.co.uk
Description: This plugin replaces [...]
Version: 1
Author: Ned Storm
Author URI: http://aweconsulting.co.uk
*/
add_theme_support( 'post-thumbnails' );

function catch_that_image() {

  global $post, $posts;
  $first_img = '';
  ob_start();
  ob_end_clean();
  $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
  $first_img = $matches [1] [0];

  return $first_img;

}

function excerpt_link_modifier_install() {
    global $wpdb;
}

function excerpt_link_modify() {
    if ( '' == $text ) {
        $text = get_the_content('');

        $text = strip_shortcodes( $text );

        $text = apply_filters('the_content', $text);
        $text = str_replace(']]>', ']]>', $text);
        $text = strip_tags($text);
        $excerpt_length = apply_filters('excerpt_length', 60);
        $words = explode(' ', $text, $excerpt_length + 1);
        if (count($words) > $excerpt_length) {
            array_pop($words);
            array_push($words, '...');
            $text = implode(' ', $words);
        }
    }
    return $text;
}

register_activation_hook(__FILE__, 'excerpt_link_modifier_install');

add_filter('get_the_excerpt','excerpt_link_modify'); 

function excerpt_link_modify2() {
    if ( '' == $text ) {
        $text = get_the_excerpt('');

        $text = strip_shortcodes( $text );

        $text = apply_filters('the_content', $text);
        $text = str_replace(']]>', ']]>', $text);
        $text = strip_tags($text);
        $excerpt_length = apply_filters('excerpt_length', 30);
        $words = explode(' ', $text, $excerpt_length + 1);
        if (count($words) > $excerpt_length) {
            array_pop($words);
            array_push($words, '...');
            $text = implode(' ', $words);
        }
    }
    echo $text;
}

?>
<?php
// function to set trackbacks
function list_pings($comment, $args, $depth) {
       $GLOBALS['comment'] = $comment;
?>
<li id="comment-<?php comment_ID(); ?>"><?php comment_author_link(); ?>
<?php } ?>
<?php
if ( function_exists('register_sidebar') ) {
	register_sidebar(array(
    	'name' => 'Right Sidebar',
        'before_widget' => '<div class="sidebar-widget">',
        'after_widget' => '<div class="clear"></div></div>',
        'before_title' => '<h2 class="sidehead">',
        'after_title' => '</h2>',
    ));
	
	register_sidebar(array(
    	'name' => 'Footer 1',
        'before_widget' => '<div class="sidebar-widget">',
        'after_widget' => '<div class="clear"></div></div>',
        'before_title' => '<h2 class="sidehead">',
        'after_title' => '</h2>',
    ));

	register_sidebar(array(
    	'name' => 'Footer 2',
        'before_widget' => '<div class="sidebar-widget">',
        'after_widget' => '<div class="clear"></div></div>',
        'before_title' => '<h2 class="sidehead">',
        'after_title' => '</h2>',
    ));

	register_sidebar(array(
    	'name' => 'Footer 3',
        'before_widget' => '<div class="sidebar-widget">',
        'after_widget' => '<div class="clear"></div></div>',
        'before_title' => '<h2 class="sidehead">',
        'after_title' => '</h2>',
    ));
}

if (get_option('Fungus_gravatar')){
add_filter( 'avatar_defaults', 'newgravatar' );

function newgravatar ($avatar_defaults) {
    $myavatar = get_option('Fungus_gravatar');
    $avatar_defaults[$myavatar] =  get_option('Fungus_gravatar_name');
    return $avatar_defaults;
}
}

// Fungus Custom Options Page(based on designpile theme from site5.coms
//---------------------------------------------

add_action('admin_menu', 'Fungus_theme_page');
function Fungus_theme_page ()
{
	if ( count($_POST) > 0 && isset($_POST['Fungus_settings']) )
	{
		$options = array ( 'layout', 'style','logo_image', 'logo_alt','gravatar','gravatar_name','site_description','keywords','rss_link','twitter_link', 'mail_link','latest_posts', 'popular_posts','analytics', 'timthumb', 'exclude_cat', 'exclude_page');
		
		foreach ( $options as $opt )
		{
			delete_option ( 'Fungus_'.$opt, $_POST[$opt] );
			add_option ( 'Fungus_'.$opt, $_POST[$opt] );	
		}
	}
	
	add_theme_page(__('Fungus Options'), __('Fungus Options'), 'edit_themes', basename(__FILE__), 'Fungus_settings');	
}
function Fungus_settings()
{?>
<div class="wrap">
	<h2>Fungus Options Panel</h2>
	
<form method="post" action="">
	<fieldset style="border:1px solid #ddd; padding-bottom:20px; margin-top:20px;">
	<legend style="margin-left:5px; padding:0 5px;color:#2481C6; text-transform:uppercase;"><strong>General Settings</strong></legend>
	<table class="form-table">
		<!-- General settings -->


<!--		<tr valign="top">
			<th scope="row"><label for="layout">Theme Layout</label></th>
			<td>				
					<input name="layout" type="radio" value="default(right sidebar)" <?php // if(get_option('Fungus_layout') != 'left sidebar'){?>checked="checked"<?php // }?> />right sidebar(default) &nbsp;&nbsp;
					<input name="layout" type="radio" value="left sidebar" <?php // if(get_option('Fungus_layout') == 'left sidebar'){?>checked="checked"<?php // }?> />left sidebar
			</td>
		</tr> -->
		
<!--		<tr valign="top">
			<th scope="row"><label for="style">Theme Color Scheme</label></th>
			<td>
				<select name="style" id="style">
                	<option>Select theme style</option>				
					<option value="black.css" <?php // if(get_option('Fungus_style') == 'black.css'){?>selected="selected"<?php // }?>>black.css</option>			
					<option value="red.css" <?php // if(get_option('Fungus_style') == 'red.css'){?>selected="selected"<?php // }?>>red.css</option>
					<option value="green.css" <?php // if(get_option('Fungus_style') == 'green.css'){?>selected="selected"<?php // }?>>green.css</option>
                    <option value="blue.css" <?php // if(get_option('Fungus_style') == 'blue.css'){?>selected="selected"<?php // }?>>blue.css</option>
					<option value="orange.css" <?php // if(get_option('Fungus_style') == 'orange.css'){?>selected="selected"<?php // }?>>orange.css</option>
				</select> 
			</td>
		</tr> -->
		<tr valign="top">
			<th scope="row"><label for="logo_image">Change logo (full path to logo image)</label></th>
			<td>
				<input name="logo_image" type="text" id="logo_image" value="<?php echo get_option('Fungus_logo_image'); ?>" class="regular-text" /><br />
				<em>current logo:</em> <br /> <img src="<?php echo get_option('Fungus_logo_image'); ?>" alt="<?php echo get_option('Fungus_logo_alt'); ?>" />
			</td>
		</tr>
		<tr valign="top">
			<th scope="row"><label for="logo_alt">Logo ALT Text</label></th>
			<td>
				<input name="logo_alt" type="text" id="logo_alt" value="<?php if(get_option('Fungus_logo_alt')) echo get_option('Fungus_logo_alt'); else echo 'Logo';?>" class="regular-text" />
			</td>
		</tr>
		<tr valign="top">
			<th scope="row"><label for="gravatar">Change default gravatar (full path to new gravatar image)</label></th>
			<td>
				<input name="gravatar" type="text" id="gravatar" value="<?php echo get_option('Fungus_gravatar'); ?>" class="regular-text" /><br />
				<small>for better display, make sure Your new gravatar image is square</small>
			</td>
		</tr>
		<tr valign="top">
			<th scope="row"><label for="gravatar_name">New gravatar name</label></th>
			<td>
				<input name="gravatar_name" type="text" id="gravatar_name" value="<?php if (get_option('Fungus_gravatar_name'))echo get_option('Fungus_gravatar_name'); else echo 'New Gravatar'?>" class="regular-text" />
			</td>
		</tr>
		<tr valign="top">
			<th scope="row"><label for="exclude_page">Excluded page(s) from navigation</label></th>
			<td>				
					<input name="exclude_page" type="text" id="exclude_page" value="<?php echo get_option('Fungus_exclude_page'); ?>" class="regular-text" /><br />
					<small>Write page ID you want to exclude from page navigation. Below page list and their ID. Use comma to separate between pages <br />
						<?php $pages = get_pages('orderby=name'); //creating page dropdown list
								foreach($pages as $page_list){
									echo ($page_list->post_title). '('.($page_list->ID).'), ' ;
								}
						?>
					</small>


			</td>
		</tr>			
		<tr valign="top">
			<th scope="row"><label for="exclude_cat">Excluded category(ies) from navigation</label></th>
			<td>				
					<input name="exclude_cat" type="text" id="exclude_cat" value="<?php echo get_option('Fungus_exclude_cat'); ?>" class="regular-text" /><br />
					<small>write category ID you want to exclude from category navigation. You can see categories and their ID below. Use comma to separate between categories <br />
					<?php $categories = get_categories('hide_empty=0&orderby=name');
					foreach ($categories as $category_list ) {
						 echo ($category_list->cat_name).'('.($category_list->cat_ID).'), ';
					}
					?>
					</small>

			</td>
		</tr>		
	</table>
	</fieldset>
    
	<fieldset style="border:1px solid #ddd; padding-bottom:20px; margin-top:20px;">
	<legend style="margin-left:5px; padding:0 5px; color:#2481C6;text-transform:uppercase;"><strong>Sidebar Settings</strong></legend>
		<table class="form-table">
		<tr><td colspan="2">Use "http://" at the begining of URLs! ( e.g. http://www.twitter.com/youraccount )</td></tr>
		<tr valign="top">
			<th scope="row"><label for="twitter_link">Twitter link</label></th>
			<td>
				<input name="twitter_link" type="text" id="twitter_link" value="<?php echo get_option('Fungus_twitter_link'); ?>" class="regular-text" />
			</td>
		</tr>
        <tr valign="top">
			<th scope="row"><label for="rss_link">Rss feed link</label></th>
			<td>
				<input name="rss_link" type="text" id="rss_link" value="<?php echo get_option('Fungus_rss_link'); ?>" class="regular-text" />
			</td>
		</tr>
        <tr valign="top">
			<th scope="row"><label for="mail_link">E-mail feed link</label></th>
			<td>
				<input name="mail_link" type="text" id="mail_link" value="<?php echo get_option('Fungus_mail_link'); ?>" class="regular-text" /><br />
			</td>
		</tr>
<!-- 		<tr valign="top">
			<th scope="row"><label for="popular_posts">Show popular posts</label></th>
			<td>
					<input name="popular_posts" type="radio" value="yes" <?php // if(get_option('Fungus_popular_posts') != 'no'){?>checked="checked"<?php // }?> />Yes
					<input name="popular_posts" type="radio" value="no" <?php // if(get_option('Fungus_popular_posts') == 'no'){?>checked="checked"<?php // }?> />No
			</td>
		</tr>
		<tr valign="top">
			<th scope="row"><label for="popular_posts">Show latest posts</label></th>
			<td>
					<input name="latest_posts" type="radio" value="yes" <?php // if(get_option('Fungus_latest_posts') != 'no'){?>checked="checked"<?php // }?> />Yes
					<input name="latest_posts" type="radio" value="no" <?php // if(get_option('Fungus_latest_posts') == 'no'){?>checked="checked"<?php // }?> />No
			</td>
		</tr>			-->
        </table>
        </fieldset>
        
    <fieldset style="border:1px solid #ddd; padding-bottom:20px; margin-top:20px;">
	<legend style="margin-left:5px; padding:0 5px; color:#2481C6;text-transform:uppercase;"><strong>Google Analytics</strong></legend>
		<table class="form-table">
		<tr>
			<th><label for="ads">Google Analytics code:</label></th>
			<td>
				<textarea name="analytics" id="analytics" rows="7" cols="70" style="font-size:11px;"><?php echo stripslashes(get_option('Fungus_analytics')); ?></textarea>
			</td>
		</tr>
		
	</table>
	</fieldset>
	
   <fieldset style="border:1px solid #ddd; padding-bottom:20px; margin-top:20px;">
	<legend style="margin-left:5px; padding:0 5px; color:#2481C6;text-transform:uppercase;"><strong>SEO Settings</strong></legend>
		<table class="form-table">	
        <tr>
        	<td colspan="2"></td>
        </tr> 
      <tr valign="top">
			<th scope="row"><label for="site_description">Add a site description here. </label></th>
			<td>
				<textarea name="site_description" id="site_description" rows="5" cols="70" style="font-size:11px;"><?php echo stripslashes(get_option('Fungus_site_description')); ?></textarea><br />
				<small>NOTE: Content is used as a description on individual posts and pages.</small>
			</td>
		</tr>
        <tr valign="top">
			<th scope="row"><label for="keywords">Add site specific keywords here, separated by commas. </label></th>
			<td>
				<textarea name="keywords" id="keywords" rows="5" cols="70" style="font-size:11px;"><?php echo stripslashes(get_option('Fungus_keywords')); ?></textarea><br />
				<small>NOTE: Tags are used as keywords on individual post pages.</small>
			</td>
		</tr>
     </table>
     </fieldset>
	
	<p class="submit">
		<input type="submit" name="Submit" class="button-primary" value="Save Changes" />
		<input type="hidden" name="Fungus_settings" value="save" style="display:none;" />
	</p>
</form>

</div>
<?php }

// =============================== Flickr widget (based on woothemes.com widget) ======================================
function FungusflickrWidget()
{
	$settings = get_option("Fungus_widget_flickrwidget");

	$id = $settings['id'];
	$number = $settings['number'];

?>

<div id="flickr" class="widget">
	<h2 class="sidehead"><?php _e('Photos on flickr') ?></h2>
	<div class="pictures">
		<script type="text/javascript" src="http://www.flickr.com/badge_code_v2.gne?count=<?php echo $number; ?>&amp;display=latest&amp;size=s&amp;layout=x&amp;source=user&amp;user=<?php echo $id; ?>"></script>        
		<div class="clear"></div>
	</div>
</div>

<?php
}

function FungusflickrWidgetAdmin() {

	$settings = get_option("Fungus_widget_flickrwidget");

	// check if anything's been sent
	if (isset($_POST['update_flickr'])) {
		$settings['id'] = strip_tags(stripslashes($_POST['Fungus_flickr_id']));
		$settings['number'] = strip_tags(stripslashes($_POST['Fungus_flickr_number']));

		update_option("Fungus_widget_flickrwidget",$settings);
	}

	echo '<p>
			<label for="Fungus_flickr_id">Flickr ID (<a href="http://www.idgettr.com">idGettr</a>):
			<input id="Fungus_flickr_id" name="Fungus_flickr_id" type="text" class="widefat" value="'.$settings['id'].'" /></label></p>';
	echo '<p>
			<label for="Fungus_flickr_number">Number of photos:
			<input id="Fungus_flickr_number" name="Fungus_flickr_number" type="text" class="widefat" value="'.$settings['number'].'" /></label></p>';
	echo '<input type="hidden" id="update_flickr" name="update_flickr" value="1" />';

}

register_sidebar_widget('Fungus - Flickr', 'FungusflickrWidget');
register_widget_control('Fungus - Flickr', 'FungusflickrWidgetAdmin', 300, 200);

// =============================== twitter widget  ======================================
function FungustwitterWidget($args)
{
	extract($args);
	
	$settings = get_option("Fungus_widget_twitterwidget");

	$title = $settings['title'];
	$id = $settings['id'];
	$number = $settings['number'];
	
	echo $before_widget;
	echo $before_title;
	echo $title; 
	echo $after_title;
?>


<script src="http://twitter.com/javascripts/blogger.js" type="text/javascript"></script>
<script src="http://twitter.com/statuses/user_timeline/<?php echo $id; ?>.json?callback=twitterCallback2&count=<?php echo $number; ?>" type="text/javascript"></script>
<?php

	echo $after_widget; 
}

function FungustwitterWidgetAdmin() {

	$settings = get_option("Fungus_widget_twitterwidget");

	// check if anything's been sent
	if (isset($_POST['update_twitter'])) {
		$settings['title'] = strip_tags(stripslashes($_POST['Fungus_twitter_title']));
		$settings['id'] = strip_tags(stripslashes($_POST['Fungus_twitter_id']));
		$settings['number'] = strip_tags(stripslashes($_POST['Fungus_twitter_number']));

		update_option("Fungus_widget_twitterwidget",$settings);
	}
	
	echo '<p>
			<label for="Fungus_twitter_title">Title:
			<input id="Fungus_twitter_title" name="Fungus_twitter_title" type="text" class="widefat" value="'.$settings['title'].'" /></label></p>';
	echo '<p>
			<label for="Fungus_twitter_id">Twitter Username:
			<input id="Fungus_twitter_id" name="Fungus_twitter_id" type="text" class="widefat" value="'.$settings['id'].'" /></label></p>';
	echo '<p>
			<label for="Fungus_twitter_number">Maximum tweet to display :
			<input id="Fungus_twitter_number" name="Fungus_twitter_number" type="text" class="widefat" value="'.$settings['number'].'" /></label></p>';
	echo '<input type="hidden" id="update_twitter" name="update_twitter" value="1" />';

}

register_sidebar_widget('Fungus - twitter', 'FungustwitterWidget');
register_widget_control('Fungus - twitter', 'FungustwitterWidgetAdmin', 300, 200);


// Tags for keywords based on magazine basic from bavotasan.com
function FungusmetaTag() {
    $posttags = get_the_tags();
    if($posttags) {
		foreach((array)$posttags as $tag) {
			$Fungus_tags .= $tag->name . ',';
		}
	}
    echo '<meta name="keywords" content="'.$Fungus_tags.get_option('Fungus_keywords').'" />';
}

// Meta description based on magazine basic from bavotasan.com
function FungusmetaDesc() {
	$content = strip_tags(get_the_content());
	if($content) {
		$content = preg_replace('/\[.+\]/','', $content);
		$content = ereg_replace("[\n\r]", "\t", $content);
		$content = ereg_replace("\t\t+", " ", $content);
	} else {
		$content = get_option('Fungus_site_description');
	}
	if (strlen($content) < 155) {
		echo $content;
	} else {
		$desc = substr($content,0,155);
		echo $desc."...";
	}
}

?>